#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       sin título.py
#       
#       Copyright 2012 Santiago Banchero <santiago@geofreedom>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
#       
#       

import socket
import select

class Mensajes:
    def __init__(self): pass
    
    def welcome_conexion(self, *args):
        return """Bienvenido %s:%i"""%(args[0])
    
    def init_conexion(self, *args):
        return """Servidor escuchando en %s:%i"""%(args[0], args[1])
    



class TerminalSrv:
    def __init__(self, host='localhost', port=9001, backlog=3):
        self.host = host
        self.port = port
        self.sesiones = {}
        self.msg = Mensajes()
        
        self.sock_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        
        self.sock_server.bind((self.host, self.port))
        
        print self.msg.init_conexion( self.host, self.port)
        self.sock_server.listen(backlog)
        
        self.inputs = [self.sock_server]
        self.outputs = []
        
        
    def start(self):
        
        flag_run = 1
        
        while flag_run:
            socks_in,socks_out,socks_err = select.select(self.inputs, self.outputs, [])
            
            for s_in in socks_in:
                if s_in == self.sock_server:
                    cliente, address = self.sock_server.accept()
                    self.inputs.append(cliente)
                    
                    cliente.send(self.msg.welcome_conexion(address))
                    if !self.sesiones.has_key(address[0]):
                        self.sesiones[address[0]] = {}
                    
                    self.outputs.append(cliente)
                else:
                    #Recibe los cmds
                    
                    try:
                        data = s_in.recv(1024)
                        if data:
                            print 'llegan datos:',data
                            
                        else:
                            s_in.close()
                            self.inputs.remove(s_in)
                            self.outputs.remove(s_in)
                            
                            msg = 'ssssssssss'
                            for s_out in self.outputs:
                                s_out.send(msg)
                                
                    except socket.error, e:
                        self.inputs.remove(s_in)
                        self.outputs.remove(s_in)
                        
                
                

def main():
    servidor = TerminalSrv()
    servidor.start()
    return 0

if __name__ == '__main__':
	main()

